package com.warmheart.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.warmheart.base.annotation.Auth;
import com.warmheart.base.controller.BaseController;
import com.warmheart.core.enums.ResultCode;
import com.warmheart.core.result.CommonResult;
import com.warmheart.core.util.ParamMapUtil;
import com.warmheart.core.util.cache.AreaCacheService;
import com.warmheart.entity.sys.SysAreaBase;
import com.warmheart.model.AreaInfoModel;
import com.warmheart.model.AreaTreeModel;
import com.warmheart.service.sys.ISysAreaBaseService;

/**
 * 
 * @ClassName: AreaController
 * @Description: 区域控制器
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@RestController
@RequestMapping("/area")
public class AreaController extends BaseController {

    // 注入数据缓存service
    @Autowired(required = false)
    private AreaCacheService areaCacheService;

    @Autowired(required = false)
    private ISysAreaBaseService sysAreaBaseService;

    /**
     * 
     * @Title: getTree
     * @Description: 获取区域树形结构
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getTree")
    public CommonResult<List<AreaTreeModel>> getTree() {
        List<AreaTreeModel> treeList = areaCacheService.getAreaCacheList();
        if (treeList == null) {
            treeList = new ArrayList<AreaTreeModel>();
        }
        return CommonResult.success("获取区域树形结构成功", treeList);
    }

    /**
     * 
     * @Title: getTwoTree
     * @Description: 获取二级区域树形结构
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getTwoTree")
    public CommonResult<List<AreaTreeModel>> getTwoTree() {
        List<AreaTreeModel> treeListTwo = areaCacheService.getTwoAreaCacheList();
        if (treeListTwo == null) {
            treeListTwo = new ArrayList<AreaTreeModel>();
        }
        return CommonResult.success("获取二级区域树形结构成功", treeListTwo);
    }

    /**
     * 
     * @Title: getAreaInfo
     * @Description: 根据区域CODE获取区域链的信息
     * @author ZhangJianyang
     * @param areaCode
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getAreaInfo")
    public CommonResult<AreaInfoModel> getAreaInfo(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String areaCode = ParamMapUtil.getString(jsonMap, "areaCode");
        if (StringUtils.isBlank(areaCode)) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "区域CODE不可为空");
        }
        AreaInfoModel areaInfoModel = areaCacheService.getAreaInfo(areaCode);
        if (areaInfoModel == null) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "区域CODE无效");
        }
        return CommonResult.success("根据区域CODE获取区域链的信息成功", areaInfoModel);
    }

    /**
     * 
     * @Title: getAreaName
     * @Description: 根据区域CODE获取区域名称
     * @author ZhangJianyang
     * @param areaCode
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getAreaName")
    public CommonResult<String> getAreaName(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String areaCode = ParamMapUtil.getString(jsonMap, "areaCode");
        if (StringUtils.isBlank(areaCode)) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "区域CODE不可为空");
        }
        StringBuffer currAreaNameSb = new StringBuffer();
        AreaInfoModel areaInfo = areaCacheService.getAreaInfo(areaCode);
        if (areaInfo == null) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "区域CODE无效");
        }
        if (StringUtils.isNotBlank(areaInfo.getProvName())) {
            currAreaNameSb.append(areaInfo.getProvName());
        }
        if (StringUtils.isNotBlank(areaInfo.getCityName())) {
            currAreaNameSb.append(areaInfo.getCityName());
        }
        if (StringUtils.isNotBlank(areaInfo.getDistrictName())) {
            currAreaNameSb.append(areaInfo.getDistrictName());
        }
        return CommonResult.success("根据区域CODE获取区域名称成功", currAreaNameSb.toString());
    }

    /**
     * 
     * @Title: getProvList
     * @Description: 获取省列表
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getProvList")
    public CommonResult<List<Map<String, String>>> getProvList() {
        List<AreaInfoModel> provModel = areaCacheService.getProvList();
        List<Map<String, String>> provList = new ArrayList<>();
        if (provModel != null && provModel.size() > 0) {
            for (AreaInfoModel areaInfoModel : provModel) {
                Map<String, String> provMap = new HashMap<>();
                provMap.put("value", areaInfoModel.getProvCode());
                provMap.put("label", areaInfoModel.getProvName());
                provList.add(provMap);
            }
        }
        return CommonResult.success("获取省列表成功", provList);
    }

    /**
     * 
     * @Title: getCityList
     * @Description: 根据区域CODE获取市列表
     * @author ZhangJianyang
     * @param areaCode
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getCityList")
    public CommonResult<List<AreaTreeModel>> getCityList(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String areaCode = ParamMapUtil.getString(jsonMap, "areaCode");
        if (StringUtils.isBlank(areaCode)) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "区域CODE不可为空");
        }
        List<AreaTreeModel> cityList = areaCacheService.getProvNext(areaCode);
        if (cityList == null) {
            cityList = new ArrayList<>();
        }
        return CommonResult.success("根据区域CODE获取市列表成功", cityList);
    }

    /**
     * 
     * @Title: getDistrictList
     * @Description: 根据区域CODE获取区列表
     * @author ZhangJianyang
     * @param areaCode
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getDistrictList")
    public CommonResult<List<AreaTreeModel>> getDistrictList(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String areaCode = ParamMapUtil.getString(jsonMap, "areaCode");
        if (StringUtils.isBlank(areaCode)) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "区域CODE不可为空");
        }
        List<AreaTreeModel> districtList = areaCacheService.getCityNext(areaCode);
        if (districtList == null) {
            districtList = new ArrayList<>();
        }
        return CommonResult.success("根据区域CODE获取区列表成功", districtList);
    }

    /**
     * 
     * @Title: getProvCityMap
     * @Description: 获取省市对应数据
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getProvCityMap")
    public CommonResult<Map<String, List<AreaTreeModel>>> getProvCityMap() {
        Map<String, List<AreaTreeModel>> provCityMap = areaCacheService.getProvNext();
        if (provCityMap == null) {
            provCityMap = new HashMap<>();
        }
        return CommonResult.success("获取省市对应数据成功", provCityMap);
    }

    /**
     * 
     * @Title: getCityDistrictMap
     * @Description: 获取市区对应数据
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getCityDistrictMap")
    public CommonResult<Map<String, List<AreaTreeModel>>> getCityDistrictMap() {
        Map<String, List<AreaTreeModel>> cityDistrictMap = areaCacheService.getCityNext();
        if (cityDistrictMap == null) {
            cityDistrictMap = new HashMap<>();
        }
        return CommonResult.success("获取市区对应数据成功", cityDistrictMap);
    }

    /**
     * 
     * @Title: getCityCode
     * @Description: 根据城市名称获取城市CODE
     * @author ZhangJianyang
     * @param cityName
     * @return
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getCityCode")
    public CommonResult<String> getCityCode(HttpServletRequest request) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        String cityName = ParamMapUtil.getString(jsonMap, "cityName");
        if (StringUtils.isBlank(cityName)) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "城市名称不可为空");
        }
        String cityCode = "";
        if (StringUtils.isNotBlank(cityName)) {
            SysAreaBase sysAreaBase = new SysAreaBase();
            sysAreaBase.setAreaName(cityName);
            List<SysAreaBase> sysAreaBaseList = sysAreaBaseService.selectList(sysAreaBase);
            if (sysAreaBaseList != null && sysAreaBaseList.size() > 0) {
                cityCode = sysAreaBaseList.get(0).getCode();
            }
        }
        return CommonResult.success("根据城市名称获取城市CODE成功", cityCode);
    }

    /**
     * 
     * @Title: getAreaNameMap
     * @Description: 获取地区名称MAP
     * @author ZhangJianyang
     * @return
     */
    @Auth(verifyLogin = true)
    @PostMapping(value = "/getAreaNameMap")
    public CommonResult<Map<String, String>> getAreaNameMap() {
        Map<String, String> areaNameMap = new HashMap<String, String>();
        Map<String, AreaInfoModel> areaMap = areaCacheService.getAreaInfo();
        if (!areaMap.isEmpty()) {
            for (Entry<String, AreaInfoModel> entry : areaMap.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    StringBuffer currAreaNameSb = new StringBuffer();
                    if (StringUtils.isNotBlank(entry.getValue().getProvName())) {
                        currAreaNameSb.append(entry.getValue().getProvName());
                    }
                    if (StringUtils.isNotBlank(entry.getValue().getCityName())) {
                        currAreaNameSb.append(entry.getValue().getCityName());
                    }
                    if (StringUtils.isNotBlank(entry.getValue().getDistrictName())) {
                        currAreaNameSb.append(entry.getValue().getDistrictName());
                    }
                    if (StringUtils.isNotBlank(entry.getValue().getDistrictCode())) {
                        areaNameMap.put(entry.getValue().getDistrictCode(), currAreaNameSb.toString());
                    } else if (StringUtils.isNotBlank(entry.getValue().getCityCode())) {
                        areaNameMap.put(entry.getValue().getCityCode(), currAreaNameSb.toString());
                    } else if (StringUtils.isNotBlank(entry.getValue().getProvCode())) {
                        areaNameMap.put(entry.getValue().getProvCode(), currAreaNameSb.toString());
                    }
                }
            }
        }
        return CommonResult.success("获取地区名称MAP成功", areaNameMap);
    }
}
